# -- coding: utf-8 --
import pprint
from tools.Request_General import BaseAPI
from pyLib.LogManage import LOG


class Dimension(BaseAPI):
    """
    维度相关接口
    """

    def build_period_config_and_member(self, payload):
        """维度-编辑-编辑期间层级"""
        r = self.request_send(json=payload)
        return r

    def get_view_by_period(self, payload):
        """维度-编辑-获取期间属性view成员"""
        r = self.request_send(json=payload)
        return r

    def query_dimension_info(self, payload):
        """维度-编辑-获取主体信息"""
        r = self.request_send(json=payload)
        return r

    def save_dimension_info(self, payload):
        """维度-编辑-主体新建及编辑保存"""
        r = self.request_send(json=payload)
        return r

    def update_view_member(self, payload):
        """维度-编辑-更新view成员描述"""
        r = self.request_send(json=payload)
        return r

    def get_around_dimension(self, payload):
        """【特殊】获取前后第n个维度成员"""
        r = self.request_send(json=payload)
        return r

    def get_dimension_description(self, payload):
        """【特殊】获取维度成员描述"""
        r = self.request_send(json=payload)
        return r

    def get_dimension_member_by_level(self, payload):
        """【特殊】通过层级获取维度成员"""
        r = self.request_send(json=payload)
        return r

    def get_dimension_member_by_parent(self, payload):
        """【特殊】根据父级属性查维度成员"""
        r = self.request_send(json=payload)
        return r

    def get_more_level_member(self, payload):
        """【特殊】POV的多层级显示"""
        r = self.request_send(json=payload)
        return r

    def get_ud_value_by_express(self, payload):
        """【特殊】根据表达式获取维度自定义属性"""
        r = self.request_send(json=payload)
        return r

    def get_dimension_member_list(self, payload):
        """查询维度成员列表"""
        r = self.request_send(json=payload)
        return r

    def get_relation(self, payload):
        """【特殊】获取维度关联元素清单"""
        r = self.request_send(params=payload)
        return r

    def save_relation(self, payload):
        """【特殊】保存维度关联元素清单"""
        r = self.request_send(json=payload)
        return r

    def save_dimension_info_and_ud(self, payload):
        """维度新建及编辑"""
        r = self.request_send(json=payload)
        return r

    def query_date_dimension(self, payload):
        """获取期间维度"""
        r = self.request_send(json=payload)
        return r

    def find_dimension_type(self, payload):
        """【特殊】查询可新建的维度类型"""
        r = self.request_send(params=payload)
        return r

    def get_access_dimension_member_list(self, payload):
        """维度成员不分层级搜索"""
        r = self.request_send(json=payload)
        return r

    def get_all_year_period_of_sceanrio(self, payload):
        """根据开始结束年期间返回所有组合"""
        r = self.request_send(json=payload)
        return r

    def get_dimension_list(self, payload):
        """获取维度信息"""
        r = self.request_send(json=payload)
        return r

    def get_member_level(self, payload):
        """维度成员层级查询"""
        r = self.request_send(json=payload)
        return r

    def get_multi_dimension_description(self, payload):
        """查询多组维度成员描述"""
        r = self.request_send(json=payload)
        return r

    def get_open_period_config(self, payload):
        """查询期间维度配置列表"""
        r = self.request_send(json=payload)
        return r

    def get_period_by_level(self, payload):
        """根据期间层级查询期间维度成员"""
        r = self.request_send(params=payload)
        return r

    def get_view_by_express(self, payload):
        """根据view表达式,period维度名,folderId查询view成员"""
        r = self.request_send(json=payload)
        return r

    def batch_get_view_by_express(self, payload):
        """批量查询view成员"""
        r = self.request_send(json=payload)
        return r

    def query_view_by_period(self, payload):
        """根据period维度名,folderId查询所有view维度成员"""
        r = self.request_send(json=payload)
        return r

    def open_dimension_info_by_id(self, payload):
        """打开维度主体信息"""
        r = self.request_send(json=payload)
        return r

    def select_dimension_member(self, payload):
        """查询单个维度成员信息"""
        r = self.request_send(params=payload)
        return r

    def select_dimension_member_by_name_function(self, payload):
        """通过表达式查询维度成员"""
        r = self.request_send(json=payload)
        return r

    def batch_select_dimension_member_by_name_function(self, payload):
        """通过表达式查询维度成员 - 批量"""
        r = self.request_send(json=payload)
        return r

    def check_and_save_dimension_member(self, payload):
        """校验维度成员是否存在并保存"""
        r = self.request_send(json=payload)
        return r

    def get_page_dimension_tree(self, payload):
        """维度树查询"""
        r = self.request_send(json=payload)
        return r

    def save_dimension_and_member(self, payload):
        """保存维度和成员"""
        r = self.request_send(json=payload)
        return r

    def save_verify(self, payload):
        """校验维度是否存在"""
        r = self.request_send(json=payload)
        return r

    def judge_field_empty(self, payload):
        """维度-数据-判断维度数据某列是否有值"""
        r = self.request_send(params=payload)
        return r

    def get_dimension_member(self, payload):
        """维度-数据-维度成员单个查询"""
        r = self.request_send(json=payload)
        return r

    def get_dimension_member_by_ids(self, payload):
        """维度-数据-维度成员批量查询，通用接口 by 维度成员id"""
        r = self.request_send(json=payload)
        return r

    def get_dimension_member_for_table(self, payload):
        """
        维度-数据-维度成员表格清单查询
        通用接口 （维度表格编辑视图 - 全量/增量编辑处使用）剔除了被动共享的维度成员
        """
        r = self.request_send(json=payload)
        return r

    def get_dimension_member_list_access(self, payload):
        """
        维度-数据-维度成员平铺查询 - 不分页
        - 1.查询维度(dimensionName)下维度成员 如传了维度表达式(dimensionExpression) 则根据表达式限制查询范围
        - 2.模糊搜索：根据关键字(searchValue)&搜索类型(searchType)模糊搜索
        - 3.是否返回维度成员路径(member)是处理成员在树形结构中的路径 反参中以path返回具体路径  PS.若未传fieldList则默认查询所有字段，若想查询当前语言 可传description若未传fieldList则默认查询所有字段，若想查询当前语言 可传description
        """
        r = self.request_send(json=payload)
        return r

    def get_dimension_member_list_access_page(self, payload):
        """
        维度成员不分层级搜索，通用分页查询
        - 1.查询维度(dimensionName)下维度成员 如传了维度表达式(dimensionExpression) 则根据表达式限制查询范围
        - 2.模糊搜索：根据关键字(searchValue)&搜索类型(searchType)模糊搜索
        - 3.是否返回维度成员路径(member)是处理成员在树形结构中的路径 反参中以path返回具体路径- 4.默认选中成员(defaultValue)在查询结果中找到你想默认选中成员 并将其所在页以及之前页码所有信息全返回 选中的成员defaultSelected为True  PS.若未传fieldList则默认查询所有字段，若想查询当前语言 可传description若未传fieldList则默认查询所有字段，若想查询当前语言 可传description
        """
        r = self.request_send(json=payload)
        return r

    def get_dimension_tree(self, payload):
        """
        维度-数据-维度树查询 - 不分页
        - 1.查询维度(dimensionName)下维度成员 如传了维度表达式(dimensionExpression) 则根据表达式限制查询范围
        - 2.查询逻辑:根据当前选中节点(parentNodeId)查询下一层信息
        PS.若未传当前选中节点(parentNodeId) 默认从根节点开始查询
        若未传fieldList则默认查询所有字段，若想查询当前语言 可传description
       """
        r = self.request_send(json=payload)
        return r

    def get_dimension_tree_page(self, payload):
        """
        维度-数据-维度树查询 - 分页
        - 1.查询维度(dimensionName)下维度成员 如传了维度表达式(dimensionExpression) 则根据表达式限制查询范围
        - 2.查询逻辑优先级：
            1)默认值：查询默认值(defaultValue)所在树形结构 不受页码影响 会根据pagesize展示到满足条件的维度成员层级
            2)根据起始节点(startNodeId)和父级节点(parentNodeId)查询父级节点下从起始节点到pagesize之间的信息 *这个场景一般前端使用
            3)根据当前选中节点(parentNodeId)查询下一层信息
            PS.若未传当前选中节点(parentNodeId) 默认从根节点开始查询
               若未传fieldList则默认查询所有字段，若想查询当前语言 可传description
       """
        r = self.request_send(json=payload)
        return r

    def select_dimension_expression(self, payload):
        """
        维度-数据-表达式查询维度成员
        - 1.当方法中的成员不存在时 通过参数是否忽略不存在成员(ignoreIllegalMember)决定是报错提示 还是忽略这个方法
        PS.若未传fieldList则默认查询所有字段，若想查询当前语言 可传description
       """
        r = self.request_send(json=payload)
        return r

    def select_dimension_expression_grouping_by_function(self, payload):
        """
        维度-数据-表达式查询维度成员 - 根据方法分组
        - 1.当方法中的成员不存在时 通过参数是否忽略不存在成员(ignoreIllegalMember)决定是报错提示 还是忽略这个方法
        PS.若未传fieldList则默认查询所有字段，若想查询当前语言 可传description
       """
        r = self.request_send(json=payload)
        return r

    def export_dimension_member(self, payload):
        """
        维度-数据-导出
       """
        r = self.request_send(params=payload)
        return r

    def import_dimension_member(self, payload, filepath):
        """
        维度-数据-导入
       """
        file = {"uploadFile": open(filepath, 'rb')}
        r = self.request_send(data=payload, files=file)
        return r

    def save_member(self, payload):
        """
        维度-数据-保存维度成员信息
       """
        r = self.request_send(json=payload)
        return r

    def save_tree_member(self, payload):
        """
        维度-数据-树形增量编辑维度接口
       """
        r = self.request_send(json=payload)
        return r

    def dimension_member_change_save(self, payload):
        """
        维度树形增量编辑成员
       """
        r = self.request_send(json=payload)
        return r

    def select_dimension_member_list(self, payload):
        """
        查询维度成员列表
       """
        r = self.request_send(json=payload)
        return r

    def save_dimension_member(self, payload):
        """
        保存维度成员信息
       """
        r = self.request_send(json=payload)
        return r

    def check_exists(self, payload):
        """
        维度-校验-批量查询维度是否存在
       """
        r = self.request_send(json=payload)
        return r

    def check_expresses_contains_member(self, payload):
        """
        维度-校验-批量维度成员匹配表达式
       """
        r = self.request_send(json=payload)
        return r

    def check_member_exists(self, payload):
        """
        维度-校验-批量查询维度成员是存在
       """
        r = self.request_send(json=payload)
        return r

    def check_contains_express_ud(self, payload):
        """
        维度-校验-校验成员是否在维度-属性维表达式中
       """
        r = self.request_send(json=payload)
        return r

    def batch_check_member_exists_express(self, payload):
        """
        维度-校验-批量校验表达式成员是否存在
       """
        r = self.request_send(json=payload)
        return r

    def check_member_exists_express(self, payload):
        """
        维度-校验-校验表达式成员是否存在
       """
        r = self.request_send(json=payload)
        return r

    def check_member_exists_single(self, payload):
        """
        维度-校验-查询单个维度成员是否存在
       """
        r = self.request_send(json=payload)
        return r

    def check_member_express_contains(self, payload):
        """
        维度-校验-校验成员是否在表达式中
       """
        r = self.request_send(json=payload)
        return r

    def batch_check_member_express_rs_contains(self, payload):
        """
        批量维度校验成员是否在表达式和角色方案中
       """
        r = self.request_send(json=payload)
        return r

    def batch_check_member_express_contains(self, payload):
        """
        维度-校验-批量维度校验成员是否在表达式中
       """
        r = self.request_send(json=payload)
        return r

    def get_authorized_dimension_member_level(self, payload):
        r = self.request_send(json=payload)
        return r

    def delete_cache(self, payload):
        """主动删除维度缓存"""
        r = self.request_send(json=payload)
        return r


class OperationDim:
    """维度相关操作"""

    def __init__(self, dimension: object):
        """
        :param dimension: 需要操作的维度对象
        """
        self.dimension = dimension

    def save_dimension_and_import_member(self, save_info_pay, file_path, increment="0"):
        """
        新建维度并导入成员
        :param save_info_pay: 保存维度主体信息请求
        :param file_path: 导入维度成员文件路径
        :param increment: 全量/增量导入--> 0全量，1增量
        """
        # 保存维度主体信息
        self.dimension.save_dimension_info(save_info_pay)
        # 导入维度成员
        r = self.dimension.import_dimension_member(
            payload={"dimensionName": save_info_pay.get("name"),
                     "folderId": save_info_pay.get("folderId"),
                     "increment": increment},
            filepath=file_path
        )
        return r


if __name__ == '__main__':
    # pass
    from application.general_create import COOKIE, APP_ID, SPACE_ID

    headers = {"cookie": COOKIE, "language": "zh-cn", "app": APP_ID, "space": SPACE_ID}
    dim = Dimension(version="1.2", headers=headers)
    r = dim.get_dimension_member_for_table(
        {"dimensionName":"Account","folderId":"0"}).json().get("data").get("memberList")
    result = [(one.get("name"),one.get("parentName"),one.get("descriptionEn"),one.get("descriptionZhCn"),one.get("aggweight"),one.get("sharedMember"),one.get("isActive"),one.get("dataType"),one.get("accountType"),one.get("inputOnParentNode"),one.get("calculationFormulaList"),one.get("ud1"),one.get("ud2")) for one in r]
    pprint.pprint(result)
